package com.example.administrator.ttphonehelper.process;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.example.administrator.ttphonehelper.R;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/15 0015.
 */
public class DeviceFragment extends Fragment {
    private ExpandableListView mElvDeviceInfo;
    private Map<String,List<String>> mDataMap;
    private List<String> mGroupList;
    private DeviceAdapter mAdapter;
    TelephonyManager mTelephonyManager;
    private static final String[] GROUP_NAME = new String[]{"基本信息","CPU","内存"};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device,null);
        mElvDeviceInfo = (ExpandableListView) view.findViewById(R.id.elv_device_info);
        initData();
        mAdapter = new DeviceAdapter(getActivity(),mDataMap,mGroupList);
        mElvDeviceInfo.setAdapter(mAdapter);
        return view;
    }
    //初始化数据
    private void initData() {
        mGroupList = new ArrayList<>();
        mDataMap = new HashMap<>();
        for (int i = 0; i < GROUP_NAME.length; i++) {
            mGroupList.add(GROUP_NAME[i]);
        }
        //得到手机基本信息
        List<String> normalInfo = new ArrayList<>();
        String deviceType = "手机型号:"+Build.MODEL;
        String sysType = "系统版本:"+Build.VERSION.RELEASE;
         mTelephonyManager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        String id = "手机串号:"+mTelephonyManager.getDeviceId();
        String operator = "手机运营商:"+getOperator();
        String isRoot = "是否ROOT:"+isRoot()+"";
        normalInfo.add(deviceType);
        normalInfo.add(sysType);
        normalInfo.add(id);
        normalInfo.add(operator);
        normalInfo.add(isRoot);
        //基本信息加入Map
        mDataMap.put(GROUP_NAME[0],normalInfo);
        //得到手机CPU信息
        List<String> cpuInfo = new ArrayList<>();
        String cpuName = "cpu:"+ getCPUName();
        cpuInfo.add(cpuName);
        //CPU信息加入Map
        mDataMap.put(GROUP_NAME[1],cpuInfo);
    }
    private String getOperator()
    {
        String name = "";
        String sim = mTelephonyManager.getSimOperator();
        if(sim.equals("46000") || sim.equals("46002")){
            name="中国移动";
        }else if(sim.equals("46001")){
            name="中国联通";
        }else if(sim.equals("46003")){
            name="中国电信";
        }else {
            name="其他";
        }
        return name;
    }
    public boolean isRoot() {
        Process process = null;
        DataOutputStream dos = null;
        try {
            process = Runtime.getRuntime().exec("su");// 开启super user进程
            dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes("ls /data/\n");// 写入内容 测试是否root
            dos.writeBytes("exit\n");
            dos.flush();
            process.waitFor();
        } catch (Exception e) {
            return false;// 没有root
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
                process.destroy();
            } catch (Exception e2) {
            }
        }
        return true;
    }
    //CPU名称：
    public String getCPUName() {
        String content;
        try {
            BufferedReader br = new BufferedReader(new FileReader(
                    "/proc/cpuinfo"));
             content = br.readLine();
            br.close();
            return content;
        } catch (Exception e) {
            content = "";
            e.printStackTrace();
        }
        return content;
    }
}
